package com.ftm.dao.impl;

import java.util.Date;
import java.util.List;

import com.ftm.dao.AdminDao;
import com.ftm.entity.Admin;
import com.ftm.tools.DBTools;
import com.ftm.tools.MD5Util;

/**
 * @author 郑志伟
 *
 *         0507-成改-：增加方法,修改方法,增加了密码加密 0509-成子-：增加删除管理员的方法
 */

public class AdminDaoImpl implements AdminDao {
	// 调用加密算法
	MD5Util md5 = new MD5Util();

	// 模糊查询
	@Override
	public List<Admin> getAdmin(String keywords) {
		// TODO Auto-generated method stub
		String sql = "select * from ftm_admin where adminuser like ?";
		List<Admin> list = (List<Admin>) DBTools.exQuery(sql, Admin.class, "%" + keywords + "%");
		return list;
	}

	// 增加
	@Override
	public boolean addAdmin(Admin admin) {
		// TODO Auto-generated method stub
		if (admin == null) {
			return false;
		}
		// 对密码进行加密
		String md5password = md5.getEncodeByMd5(admin.getAdminpwd());
		return DBTools.exUpdate("insert into ftm_admin values(null,?,?,?,?,?)", admin.getAdminuser(), md5password,
				admin.getAdminname(), new Date(), 0) > 0;
	}

	// 查询
	@Override
	public Admin getAdminByName(String adminuser) {
		String sql = "select adminid,adminuser,adminpwd from ftm_admin where adminuser = ?";
		List<Admin> list = (List<Admin>) DBTools.exQuery(sql, Admin.class, adminuser);

		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	// 修改
	@Override
	public boolean UpdataAdmin(Admin admin) {
		if (admin == null) {
			return false;
		}

		return DBTools.exUpdate("update ftm_admin set adminname=?,adminpwd=? where adminid=?", admin.getAdminname(),
				admin.getAdminpwd(), admin.getAdminid()) > 0;
	}

	// 查询帐号密码
	@Override
	public Admin queryAdmin(String user, String pwd) {
		String sql = "select * from ftm_admin where adminuser=? and adminpwd=?";
		List<Admin> list = (List<Admin>) DBTools.exQuery(sql, Admin.class, user, pwd);
		if (list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	// 删除管理员
	@Override
	public boolean delAdmin(String adminname) {
		String sql = "delete from ftm_admin where adminname= ?";
		return DBTools.exUpdate(sql, adminname) > 0;
	}
	//修改密码
	@Override
	public boolean UpdataPwd(String user, String pwd) {
		String md5password = md5.getEncodeByMd5(pwd);
		return DBTools.exUpdate("update ftm_admin set adminpwd=? where adminuser=?", pwd,user) > 0;
	}



}
